
import jsonpath

d = {
    "error_code": 0,
    "stu_info":[
        {
            "id": 223,
            "name": "安妮",
            "sex": "女",
            "classname": "212期",
            "data": {
                "name": "测试1",
                "data": {
                    "name": "测试2"
                }
            }
        },
        {
            "id": 224,
            "name": "景龙",
            "sex": "男",
            "classname": "212期"
        },
        {
            "id": 225,
            "name": "MS226",
            "classname": "212期"
        }
    ]
}
# name = d["stu_info"][0]["name"]
# print(name)
# # 模糊匹配  匹配所有学员name
# data1=jsonpath.jsonpath(d, "$..name")
# print("匹配所有学员name：", data1)
# # 精确匹配
# # 获取安妮的值  匹配第一个学员name
# data3=jsonpath.jsonpath(d, "$.stu_info[0].name")
# print("匹配第一个学员name:", data3)
# #  匹配第一个学员下面所有的name
# data4=jsonpath.jsonpath(d, "$.stu_info[0]..name")
# print("匹配第一个学员name:", data4)
# #匹配前两个学员的信息
# data=jsonpath.jsonpath(d, "$.stu_info[:2]")
# print("匹配前两个学员的信息：", data)
# data5=jsonpath.jsonpath(d, "$.stu_info[?(@.sex=='女')]")
# print("匹配性别为女的学生信息:", data5)
# data6=jsonpath.jsonpath(d, "$.stu_info[?(@.sex)]")
# print("匹配包含性别列的学生信息:", data6)